/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import doan.quanlycanbo.portlet.NoSuchdonviException;
import doan.quanlycanbo.portlet.model.donvi;
import doan.quanlycanbo.portlet.model.impl.donviImpl;
import doan.quanlycanbo.portlet.model.impl.donviModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the donvi service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link donviUtil} to access the donvi persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Nguyen Tuan Vinh
 * @see donviPersistence
 * @see donviUtil
 * @generated
 */
public class donviPersistenceImpl extends BasePersistenceImpl<donvi>
	implements donviPersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = donviImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_BY_GROUP = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findByGroup",
			new String[] {
				Long.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_COUNT_BY_GROUP = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countByGroup", new String[] { Long.class.getName() });
	public static final FinderPath FINDER_PATH_FIND_BY_PARENT = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findByParent",
			new String[] {
				Long.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_COUNT_BY_PARENT = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countByParent", new String[] { Long.class.getName() });
	public static final FinderPath FINDER_PATH_FIND_BY_KHOIDV = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findByKhoiDV",
			new String[] {
				Long.class.getName(),
				
			"java.lang.Integer", "java.lang.Integer",
				"com.liferay.portal.kernel.util.OrderByComparator"
			});
	public static final FinderPath FINDER_PATH_COUNT_BY_KHOIDV = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countByKhoiDV", new String[] { Long.class.getName() });
	public static final FinderPath FINDER_PATH_FETCH_BY_D_G = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
			"fetchByD_G",
			new String[] { Long.class.getName(), Long.class.getName() });
	public static final FinderPath FINDER_PATH_COUNT_BY_D_G = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countByD_G",
			new String[] { Long.class.getName(), Long.class.getName() });
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countAll", new String[0]);

	/**
	 * Caches the donvi in the entity cache if it is enabled.
	 *
	 * @param donvi the donvi to cache
	 */
	public void cacheResult(donvi donvi) {
		EntityCacheUtil.putResult(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviImpl.class, donvi.getPrimaryKey(), donvi);

		FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_D_G,
			new Object[] {
				new Long(donvi.getMadonvi()), new Long(donvi.getGroupId())
			}, donvi);
	}

	/**
	 * Caches the donvis in the entity cache if it is enabled.
	 *
	 * @param donvis the donvis to cache
	 */
	public void cacheResult(List<donvi> donvis) {
		for (donvi donvi : donvis) {
			if (EntityCacheUtil.getResult(donviModelImpl.ENTITY_CACHE_ENABLED,
						donviImpl.class, donvi.getPrimaryKey(), this) == null) {
				cacheResult(donvi);
			}
		}
	}

	/**
	 * Clears the cache for all donvis.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(donviImpl.class.getName());
		EntityCacheUtil.clearCache(donviImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the donvi.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(donvi donvi) {
		EntityCacheUtil.removeResult(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviImpl.class, donvi.getPrimaryKey());

		FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_D_G,
			new Object[] {
				new Long(donvi.getMadonvi()), new Long(donvi.getGroupId())
			});
	}

	/**
	 * Creates a new donvi with the primary key. Does not add the donvi to the database.
	 *
	 * @param madonvi the primary key for the new donvi
	 * @return the new donvi
	 */
	public donvi create(long madonvi) {
		donvi donvi = new donviImpl();

		donvi.setNew(true);
		donvi.setPrimaryKey(madonvi);

		return donvi;
	}

	/**
	 * Removes the donvi with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the donvi to remove
	 * @return the donvi that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a donvi with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the donvi with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param madonvi the primary key of the donvi to remove
	 * @return the donvi that was removed
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a donvi with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi remove(long madonvi)
		throws NoSuchdonviException, SystemException {
		Session session = null;

		try {
			session = openSession();

			donvi donvi = (donvi)session.get(donviImpl.class, new Long(madonvi));

			if (donvi == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + madonvi);
				}

				throw new NoSuchdonviException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					madonvi);
			}

			return remove(donvi);
		}
		catch (NoSuchdonviException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected donvi removeImpl(donvi donvi) throws SystemException {
		donvi = toUnwrappedModel(donvi);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.delete(session, donvi);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		donviModelImpl donviModelImpl = (donviModelImpl)donvi;

		FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_D_G,
			new Object[] {
				new Long(donviModelImpl.getMadonvi()),
				new Long(donviModelImpl.getGroupId())
			});

		EntityCacheUtil.removeResult(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviImpl.class, donvi.getPrimaryKey());

		return donvi;
	}

	public donvi updateImpl(doan.quanlycanbo.portlet.model.donvi donvi,
		boolean merge) throws SystemException {
		donvi = toUnwrappedModel(donvi);

		boolean isNew = donvi.isNew();

		donviModelImpl donviModelImpl = (donviModelImpl)donvi;

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, donvi, merge);

			donvi.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(donviModelImpl.ENTITY_CACHE_ENABLED,
			donviImpl.class, donvi.getPrimaryKey(), donvi);

		if (!isNew &&
				((donvi.getMadonvi() != donviModelImpl.getOriginalMadonvi()) ||
				(donvi.getGroupId() != donviModelImpl.getOriginalGroupId()))) {
			FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_D_G,
				new Object[] {
					new Long(donviModelImpl.getOriginalMadonvi()),
					new Long(donviModelImpl.getOriginalGroupId())
				});
		}

		if (isNew ||
				((donvi.getMadonvi() != donviModelImpl.getOriginalMadonvi()) ||
				(donvi.getGroupId() != donviModelImpl.getOriginalGroupId()))) {
			FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_D_G,
				new Object[] {
					new Long(donvi.getMadonvi()), new Long(donvi.getGroupId())
				}, donvi);
		}

		return donvi;
	}

	protected donvi toUnwrappedModel(donvi donvi) {
		if (donvi instanceof donviImpl) {
			return donvi;
		}

		donviImpl donviImpl = new donviImpl();

		donviImpl.setNew(donvi.isNew());
		donviImpl.setPrimaryKey(donvi.getPrimaryKey());

		donviImpl.setMadonvi(donvi.getMadonvi());
		donviImpl.setGroupId(donvi.getGroupId());
		donviImpl.setCompanyId(donvi.getCompanyId());
		donviImpl.setLanguageId(donvi.getLanguageId());
		donviImpl.setMakhoi(donvi.getMakhoi());
		donviImpl.setDonvicha(donvi.getDonvicha());
		donviImpl.setOrgId(donvi.getOrgId());
		donviImpl.setTendonvi(donvi.getTendonvi());
		donviImpl.setDiachi(donvi.getDiachi());
		donviImpl.setDienthoai(donvi.getDienthoai());
		donviImpl.setFax(donvi.getFax());
		donviImpl.setEmail(donvi.getEmail());
		donviImpl.setWebsite(donvi.getWebsite());
		donviImpl.setHinhanh(donvi.getHinhanh());
		donviImpl.setGioithieuchung(donvi.getGioithieuchung());

		return donviImpl;
	}

	/**
	 * Finds the donvi with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the donvi to find
	 * @return the donvi
	 * @throws com.liferay.portal.NoSuchModelException if a donvi with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the donvi with the primary key or throws a {@link doan.quanlycanbo.portlet.NoSuchdonviException} if it could not be found.
	 *
	 * @param madonvi the primary key of the donvi to find
	 * @return the donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a donvi with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi findByPrimaryKey(long madonvi)
		throws NoSuchdonviException, SystemException {
		donvi donvi = fetchByPrimaryKey(madonvi);

		if (donvi == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + madonvi);
			}

			throw new NoSuchdonviException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				madonvi);
		}

		return donvi;
	}

	/**
	 * Finds the donvi with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the donvi to find
	 * @return the donvi, or <code>null</code> if a donvi with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the donvi with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param madonvi the primary key of the donvi to find
	 * @return the donvi, or <code>null</code> if a donvi with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi fetchByPrimaryKey(long madonvi) throws SystemException {
		donvi donvi = (donvi)EntityCacheUtil.getResult(donviModelImpl.ENTITY_CACHE_ENABLED,
				donviImpl.class, madonvi, this);

		if (donvi == null) {
			Session session = null;

			try {
				session = openSession();

				donvi = (donvi)session.get(donviImpl.class, new Long(madonvi));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (donvi != null) {
					cacheResult(donvi);
				}

				closeSession(session);
			}
		}

		return donvi;
	}

	/**
	 * Finds all the donvis where groupId = &#63;.
	 *
	 * @param groupId the group id to search with
	 * @return the matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findByGroup(long groupId) throws SystemException {
		return findByGroup(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the donvis where groupId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param groupId the group id to search with
	 * @param start the lower bound of the range of donvis to return
	 * @param end the upper bound of the range of donvis to return (not inclusive)
	 * @return the range of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findByGroup(long groupId, int start, int end)
		throws SystemException {
		return findByGroup(groupId, start, end, null);
	}

	/**
	 * Finds an ordered range of all the donvis where groupId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param groupId the group id to search with
	 * @param start the lower bound of the range of donvis to return
	 * @param end the upper bound of the range of donvis to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findByGroup(long groupId, int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				groupId,
				
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<donvi> list = (List<donvi>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUP,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_DONVI_WHERE);

			query.append(_FINDER_COLUMN_GROUP_GROUPID_2);

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(donviModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(groupId);

				list = (List<donvi>)QueryUtil.list(q, getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_GROUP,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUP,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Finds the first donvi in the ordered set where groupId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param groupId the group id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the first matching donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a matching donvi could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi findByGroup_First(long groupId,
		OrderByComparator orderByComparator)
		throws NoSuchdonviException, SystemException {
		List<donvi> list = findByGroup(groupId, 0, 1, orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("groupId=");
			msg.append(groupId);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchdonviException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the last donvi in the ordered set where groupId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param groupId the group id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the last matching donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a matching donvi could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi findByGroup_Last(long groupId,
		OrderByComparator orderByComparator)
		throws NoSuchdonviException, SystemException {
		int count = countByGroup(groupId);

		List<donvi> list = findByGroup(groupId, count - 1, count,
				orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("groupId=");
			msg.append(groupId);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchdonviException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the donvis before and after the current donvi in the ordered set where groupId = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param madonvi the primary key of the current donvi
	 * @param groupId the group id to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the previous, current, and next donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a donvi with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi[] findByGroup_PrevAndNext(long madonvi, long groupId,
		OrderByComparator orderByComparator)
		throws NoSuchdonviException, SystemException {
		donvi donvi = findByPrimaryKey(madonvi);

		Session session = null;

		try {
			session = openSession();

			donvi[] array = new donviImpl[3];

			array[0] = getByGroup_PrevAndNext(session, donvi, groupId,
					orderByComparator, true);

			array[1] = donvi;

			array[2] = getByGroup_PrevAndNext(session, donvi, groupId,
					orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected donvi getByGroup_PrevAndNext(Session session, donvi donvi,
		long groupId, OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_DONVI_WHERE);

		query.append(_FINDER_COLUMN_GROUP_GROUPID_2);

		if (orderByComparator != null) {
			String[] orderByFields = orderByComparator.getOrderByFields();

			if (orderByFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(donviModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		qPos.add(groupId);

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByValues(donvi);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<donvi> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Finds all the donvis where donvicha = &#63;.
	 *
	 * @param donvicha the donvicha to search with
	 * @return the matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findByParent(long donvicha) throws SystemException {
		return findByParent(donvicha, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the donvis where donvicha = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param donvicha the donvicha to search with
	 * @param start the lower bound of the range of donvis to return
	 * @param end the upper bound of the range of donvis to return (not inclusive)
	 * @return the range of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findByParent(long donvicha, int start, int end)
		throws SystemException {
		return findByParent(donvicha, start, end, null);
	}

	/**
	 * Finds an ordered range of all the donvis where donvicha = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param donvicha the donvicha to search with
	 * @param start the lower bound of the range of donvis to return
	 * @param end the upper bound of the range of donvis to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findByParent(long donvicha, int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				donvicha,
				
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<donvi> list = (List<donvi>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_PARENT,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_DONVI_WHERE);

			query.append(_FINDER_COLUMN_PARENT_DONVICHA_2);

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(donviModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(donvicha);

				list = (List<donvi>)QueryUtil.list(q, getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_PARENT,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_PARENT,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Finds the first donvi in the ordered set where donvicha = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param donvicha the donvicha to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the first matching donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a matching donvi could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi findByParent_First(long donvicha,
		OrderByComparator orderByComparator)
		throws NoSuchdonviException, SystemException {
		List<donvi> list = findByParent(donvicha, 0, 1, orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("donvicha=");
			msg.append(donvicha);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchdonviException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the last donvi in the ordered set where donvicha = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param donvicha the donvicha to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the last matching donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a matching donvi could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi findByParent_Last(long donvicha,
		OrderByComparator orderByComparator)
		throws NoSuchdonviException, SystemException {
		int count = countByParent(donvicha);

		List<donvi> list = findByParent(donvicha, count - 1, count,
				orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("donvicha=");
			msg.append(donvicha);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchdonviException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the donvis before and after the current donvi in the ordered set where donvicha = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param madonvi the primary key of the current donvi
	 * @param donvicha the donvicha to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the previous, current, and next donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a donvi with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi[] findByParent_PrevAndNext(long madonvi, long donvicha,
		OrderByComparator orderByComparator)
		throws NoSuchdonviException, SystemException {
		donvi donvi = findByPrimaryKey(madonvi);

		Session session = null;

		try {
			session = openSession();

			donvi[] array = new donviImpl[3];

			array[0] = getByParent_PrevAndNext(session, donvi, donvicha,
					orderByComparator, true);

			array[1] = donvi;

			array[2] = getByParent_PrevAndNext(session, donvi, donvicha,
					orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected donvi getByParent_PrevAndNext(Session session, donvi donvi,
		long donvicha, OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_DONVI_WHERE);

		query.append(_FINDER_COLUMN_PARENT_DONVICHA_2);

		if (orderByComparator != null) {
			String[] orderByFields = orderByComparator.getOrderByFields();

			if (orderByFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(donviModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		qPos.add(donvicha);

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByValues(donvi);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<donvi> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Finds all the donvis where makhoi = &#63;.
	 *
	 * @param makhoi the makhoi to search with
	 * @return the matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findByKhoiDV(long makhoi) throws SystemException {
		return findByKhoiDV(makhoi, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the donvis where makhoi = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param makhoi the makhoi to search with
	 * @param start the lower bound of the range of donvis to return
	 * @param end the upper bound of the range of donvis to return (not inclusive)
	 * @return the range of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findByKhoiDV(long makhoi, int start, int end)
		throws SystemException {
		return findByKhoiDV(makhoi, start, end, null);
	}

	/**
	 * Finds an ordered range of all the donvis where makhoi = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param makhoi the makhoi to search with
	 * @param start the lower bound of the range of donvis to return
	 * @param end the upper bound of the range of donvis to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findByKhoiDV(long makhoi, int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				makhoi,
				
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<donvi> list = (List<donvi>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_KHOIDV,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;

			if (orderByComparator != null) {
				query = new StringBundler(3 +
						(orderByComparator.getOrderByFields().length * 3));
			}
			else {
				query = new StringBundler(3);
			}

			query.append(_SQL_SELECT_DONVI_WHERE);

			query.append(_FINDER_COLUMN_KHOIDV_MAKHOI_2);

			if (orderByComparator != null) {
				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);
			}

			else {
				query.append(donviModelImpl.ORDER_BY_JPQL);
			}

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(makhoi);

				list = (List<donvi>)QueryUtil.list(q, getDialect(), start, end);
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_BY_KHOIDV,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_KHOIDV,
						finderArgs, list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Finds the first donvi in the ordered set where makhoi = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param makhoi the makhoi to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the first matching donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a matching donvi could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi findByKhoiDV_First(long makhoi,
		OrderByComparator orderByComparator)
		throws NoSuchdonviException, SystemException {
		List<donvi> list = findByKhoiDV(makhoi, 0, 1, orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("makhoi=");
			msg.append(makhoi);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchdonviException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the last donvi in the ordered set where makhoi = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param makhoi the makhoi to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the last matching donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a matching donvi could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi findByKhoiDV_Last(long makhoi,
		OrderByComparator orderByComparator)
		throws NoSuchdonviException, SystemException {
		int count = countByKhoiDV(makhoi);

		List<donvi> list = findByKhoiDV(makhoi, count - 1, count,
				orderByComparator);

		if (list.isEmpty()) {
			StringBundler msg = new StringBundler(4);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("makhoi=");
			msg.append(makhoi);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			throw new NoSuchdonviException(msg.toString());
		}
		else {
			return list.get(0);
		}
	}

	/**
	 * Finds the donvis before and after the current donvi in the ordered set where makhoi = &#63;.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param madonvi the primary key of the current donvi
	 * @param makhoi the makhoi to search with
	 * @param orderByComparator the comparator to order the set by
	 * @return the previous, current, and next donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a donvi with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi[] findByKhoiDV_PrevAndNext(long madonvi, long makhoi,
		OrderByComparator orderByComparator)
		throws NoSuchdonviException, SystemException {
		donvi donvi = findByPrimaryKey(madonvi);

		Session session = null;

		try {
			session = openSession();

			donvi[] array = new donviImpl[3];

			array[0] = getByKhoiDV_PrevAndNext(session, donvi, makhoi,
					orderByComparator, true);

			array[1] = donvi;

			array[2] = getByKhoiDV_PrevAndNext(session, donvi, makhoi,
					orderByComparator, false);

			return array;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected donvi getByKhoiDV_PrevAndNext(Session session, donvi donvi,
		long makhoi, OrderByComparator orderByComparator, boolean previous) {
		StringBundler query = null;

		if (orderByComparator != null) {
			query = new StringBundler(6 +
					(orderByComparator.getOrderByFields().length * 6));
		}
		else {
			query = new StringBundler(3);
		}

		query.append(_SQL_SELECT_DONVI_WHERE);

		query.append(_FINDER_COLUMN_KHOIDV_MAKHOI_2);

		if (orderByComparator != null) {
			String[] orderByFields = orderByComparator.getOrderByFields();

			if (orderByFields.length > 0) {
				query.append(WHERE_AND);
			}

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN_HAS_NEXT);
					}
					else {
						query.append(WHERE_LESSER_THAN_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(WHERE_GREATER_THAN);
					}
					else {
						query.append(WHERE_LESSER_THAN);
					}
				}
			}

			query.append(ORDER_BY_CLAUSE);

			for (int i = 0; i < orderByFields.length; i++) {
				query.append(_ORDER_BY_ENTITY_ALIAS);
				query.append(orderByFields[i]);

				if ((i + 1) < orderByFields.length) {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC_HAS_NEXT);
					}
					else {
						query.append(ORDER_BY_DESC_HAS_NEXT);
					}
				}
				else {
					if (orderByComparator.isAscending() ^ previous) {
						query.append(ORDER_BY_ASC);
					}
					else {
						query.append(ORDER_BY_DESC);
					}
				}
			}
		}

		else {
			query.append(donviModelImpl.ORDER_BY_JPQL);
		}

		String sql = query.toString();

		Query q = session.createQuery(sql);

		q.setFirstResult(0);
		q.setMaxResults(2);

		QueryPos qPos = QueryPos.getInstance(q);

		qPos.add(makhoi);

		if (orderByComparator != null) {
			Object[] values = orderByComparator.getOrderByValues(donvi);

			for (Object value : values) {
				qPos.add(value);
			}
		}

		List<donvi> list = q.list();

		if (list.size() == 2) {
			return list.get(1);
		}
		else {
			return null;
		}
	}

	/**
	 * Finds the donvi where madonvi = &#63; and groupId = &#63; or throws a {@link doan.quanlycanbo.portlet.NoSuchdonviException} if it could not be found.
	 *
	 * @param madonvi the madonvi to search with
	 * @param groupId the group id to search with
	 * @return the matching donvi
	 * @throws doan.quanlycanbo.portlet.NoSuchdonviException if a matching donvi could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi findByD_G(long madonvi, long groupId)
		throws NoSuchdonviException, SystemException {
		donvi donvi = fetchByD_G(madonvi, groupId);

		if (donvi == null) {
			StringBundler msg = new StringBundler(6);

			msg.append(_NO_SUCH_ENTITY_WITH_KEY);

			msg.append("madonvi=");
			msg.append(madonvi);

			msg.append(", groupId=");
			msg.append(groupId);

			msg.append(StringPool.CLOSE_CURLY_BRACE);

			if (_log.isWarnEnabled()) {
				_log.warn(msg.toString());
			}

			throw new NoSuchdonviException(msg.toString());
		}

		return donvi;
	}

	/**
	 * Finds the donvi where madonvi = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
	 *
	 * @param madonvi the madonvi to search with
	 * @param groupId the group id to search with
	 * @return the matching donvi, or <code>null</code> if a matching donvi could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi fetchByD_G(long madonvi, long groupId)
		throws SystemException {
		return fetchByD_G(madonvi, groupId, true);
	}

	/**
	 * Finds the donvi where madonvi = &#63; and groupId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
	 *
	 * @param madonvi the madonvi to search with
	 * @param groupId the group id to search with
	 * @return the matching donvi, or <code>null</code> if a matching donvi could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public donvi fetchByD_G(long madonvi, long groupId,
		boolean retrieveFromCache) throws SystemException {
		Object[] finderArgs = new Object[] { madonvi, groupId };

		Object result = null;

		if (retrieveFromCache) {
			result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_D_G,
					finderArgs, this);
		}

		if (result == null) {
			StringBundler query = new StringBundler(4);

			query.append(_SQL_SELECT_DONVI_WHERE);

			query.append(_FINDER_COLUMN_D_G_MADONVI_2);

			query.append(_FINDER_COLUMN_D_G_GROUPID_2);

			query.append(donviModelImpl.ORDER_BY_JPQL);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(madonvi);

				qPos.add(groupId);

				List<donvi> list = q.list();

				result = list;

				donvi donvi = null;

				if (list.isEmpty()) {
					FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_D_G,
						finderArgs, list);
				}
				else {
					donvi = list.get(0);

					cacheResult(donvi);

					if ((donvi.getMadonvi() != madonvi) ||
							(donvi.getGroupId() != groupId)) {
						FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_D_G,
							finderArgs, donvi);
					}
				}

				return donvi;
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (result == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_D_G,
						finderArgs);
				}

				closeSession(session);
			}
		}
		else {
			if (result instanceof List<?>) {
				return null;
			}
			else {
				return (donvi)result;
			}
		}
	}

	/**
	 * Finds all the donvis.
	 *
	 * @return the donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the donvis.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of donvis to return
	 * @param end the upper bound of the range of donvis to return (not inclusive)
	 * @return the range of donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findAll(int start, int end) throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the donvis.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of donvis to return
	 * @param end the upper bound of the range of donvis to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of donvis
	 * @throws SystemException if a system exception occurred
	 */
	public List<donvi> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<donvi> list = (List<donvi>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			StringBundler query = null;
			String sql = null;

			if (orderByComparator != null) {
				query = new StringBundler(2 +
						(orderByComparator.getOrderByFields().length * 3));

				query.append(_SQL_SELECT_DONVI);

				appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
					orderByComparator);

				sql = query.toString();
			}
			else {
				sql = _SQL_SELECT_DONVI.concat(donviModelImpl.ORDER_BY_JPQL);
			}

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<donvi>)QueryUtil.list(q, getDialect(), start,
							end, false);

					Collections.sort(list);
				}
				else {
					list = (List<donvi>)QueryUtil.list(q, getDialect(), start,
							end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					FinderCacheUtil.removeResult(FINDER_PATH_FIND_ALL,
						finderArgs);
				}
				else {
					cacheResult(list);

					FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs,
						list);
				}

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the donvis where groupId = &#63; from the database.
	 *
	 * @param groupId the group id to search with
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByGroup(long groupId) throws SystemException {
		for (donvi donvi : findByGroup(groupId)) {
			remove(donvi);
		}
	}

	/**
	 * Removes all the donvis where donvicha = &#63; from the database.
	 *
	 * @param donvicha the donvicha to search with
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByParent(long donvicha) throws SystemException {
		for (donvi donvi : findByParent(donvicha)) {
			remove(donvi);
		}
	}

	/**
	 * Removes all the donvis where makhoi = &#63; from the database.
	 *
	 * @param makhoi the makhoi to search with
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByKhoiDV(long makhoi) throws SystemException {
		for (donvi donvi : findByKhoiDV(makhoi)) {
			remove(donvi);
		}
	}

	/**
	 * Removes the donvi where madonvi = &#63; and groupId = &#63; from the database.
	 *
	 * @param madonvi the madonvi to search with
	 * @param groupId the group id to search with
	 * @throws SystemException if a system exception occurred
	 */
	public void removeByD_G(long madonvi, long groupId)
		throws NoSuchdonviException, SystemException {
		donvi donvi = findByD_G(madonvi, groupId);

		remove(donvi);
	}

	/**
	 * Removes all the donvis from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (donvi donvi : findAll()) {
			remove(donvi);
		}
	}

	/**
	 * Counts all the donvis where groupId = &#63;.
	 *
	 * @param groupId the group id to search with
	 * @return the number of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public int countByGroup(long groupId) throws SystemException {
		Object[] finderArgs = new Object[] { groupId };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUP,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_DONVI_WHERE);

			query.append(_FINDER_COLUMN_GROUP_GROUPID_2);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(groupId);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUP,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Counts all the donvis where donvicha = &#63;.
	 *
	 * @param donvicha the donvicha to search with
	 * @return the number of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public int countByParent(long donvicha) throws SystemException {
		Object[] finderArgs = new Object[] { donvicha };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_PARENT,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_DONVI_WHERE);

			query.append(_FINDER_COLUMN_PARENT_DONVICHA_2);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(donvicha);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_PARENT,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Counts all the donvis where makhoi = &#63;.
	 *
	 * @param makhoi the makhoi to search with
	 * @return the number of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public int countByKhoiDV(long makhoi) throws SystemException {
		Object[] finderArgs = new Object[] { makhoi };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_KHOIDV,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(2);

			query.append(_SQL_COUNT_DONVI_WHERE);

			query.append(_FINDER_COLUMN_KHOIDV_MAKHOI_2);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(makhoi);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_KHOIDV,
					finderArgs, count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Counts all the donvis where madonvi = &#63; and groupId = &#63;.
	 *
	 * @param madonvi the madonvi to search with
	 * @param groupId the group id to search with
	 * @return the number of matching donvis
	 * @throws SystemException if a system exception occurred
	 */
	public int countByD_G(long madonvi, long groupId) throws SystemException {
		Object[] finderArgs = new Object[] { madonvi, groupId };

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_D_G,
				finderArgs, this);

		if (count == null) {
			StringBundler query = new StringBundler(3);

			query.append(_SQL_COUNT_DONVI_WHERE);

			query.append(_FINDER_COLUMN_D_G_MADONVI_2);

			query.append(_FINDER_COLUMN_D_G_GROUPID_2);

			String sql = query.toString();

			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(sql);

				QueryPos qPos = QueryPos.getInstance(q);

				qPos.add(madonvi);

				qPos.add(groupId);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_D_G, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Counts all the donvis.
	 *
	 * @return the number of donvis
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_DONVI);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the donvi persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.doan.quanlycanbo.portlet.model.donvi")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<donvi>> listenersList = new ArrayList<ModelListener<donvi>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<donvi>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	public void destroy() {
		EntityCacheUtil.removeCache(donviImpl.class.getName());
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST);
	}

	@BeanReference(type = khoidonviquanlyPersistence.class)
	protected khoidonviquanlyPersistence khoidonviquanlyPersistence;
	@BeanReference(type = donviPersistence.class)
	protected donviPersistence donviPersistence;
	@BeanReference(type = chucvuPersistence.class)
	protected chucvuPersistence chucvuPersistence;
	@BeanReference(type = capuyPersistence.class)
	protected capuyPersistence capuyPersistence;
	@BeanReference(type = danhhieuPersistence.class)
	protected danhhieuPersistence danhhieuPersistence;
	@BeanReference(type = dantocPersistence.class)
	protected dantocPersistence dantocPersistence;
	@BeanReference(type = hochamPersistence.class)
	protected hochamPersistence hochamPersistence;
	@BeanReference(type = hocviPersistence.class)
	protected hocviPersistence hocviPersistence;
	@BeanReference(type = tongiaoPersistence.class)
	protected tongiaoPersistence tongiaoPersistence;
	@BeanReference(type = xuatthanPersistence.class)
	protected xuatthanPersistence xuatthanPersistence;
	@BeanReference(type = giaoducphothongPersistence.class)
	protected giaoducphothongPersistence giaoducphothongPersistence;
	@BeanReference(type = ngachluongPersistence.class)
	protected ngachluongPersistence ngachluongPersistence;
	@BeanReference(type = hesoluongPersistence.class)
	protected hesoluongPersistence hesoluongPersistence;
	@BeanReference(type = hosoPersistence.class)
	protected hosoPersistence hosoPersistence;
	@BeanReference(type = sachxuatbanPersistence.class)
	protected sachxuatbanPersistence sachxuatbanPersistence;
	@BeanReference(type = dienbienhochamPersistence.class)
	protected dienbienhochamPersistence dienbienhochamPersistence;
	@BeanReference(type = dienbienhocviPersistence.class)
	protected dienbienhocviPersistence dienbienhocviPersistence;
	@BeanReference(type = dienbienluongPersistence.class)
	protected dienbienluongPersistence dienbienluongPersistence;
	@BeanReference(type = quatrinhdinuocngoaiPersistence.class)
	protected quatrinhdinuocngoaiPersistence quatrinhdinuocngoaiPersistence;
	@BeanReference(type = quatrinhnghiencuuPersistence.class)
	protected quatrinhnghiencuuPersistence quatrinhnghiencuuPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_DONVI = "SELECT donvi FROM donvi donvi";
	private static final String _SQL_SELECT_DONVI_WHERE = "SELECT donvi FROM donvi donvi WHERE ";
	private static final String _SQL_COUNT_DONVI = "SELECT COUNT(donvi) FROM donvi donvi";
	private static final String _SQL_COUNT_DONVI_WHERE = "SELECT COUNT(donvi) FROM donvi donvi WHERE ";
	private static final String _FINDER_COLUMN_GROUP_GROUPID_2 = "donvi.groupId = ?";
	private static final String _FINDER_COLUMN_PARENT_DONVICHA_2 = "donvi.donvicha = ?";
	private static final String _FINDER_COLUMN_KHOIDV_MAKHOI_2 = "donvi.makhoi = ?";
	private static final String _FINDER_COLUMN_D_G_MADONVI_2 = "donvi.madonvi = ? AND ";
	private static final String _FINDER_COLUMN_D_G_GROUPID_2 = "donvi.groupId = ?";
	private static final String _ORDER_BY_ENTITY_ALIAS = "donvi.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No donvi exists with the primary key ";
	private static final String _NO_SUCH_ENTITY_WITH_KEY = "No donvi exists with the key {";
	private static Log _log = LogFactoryUtil.getLog(donviPersistenceImpl.class);
}